<?php
date_default_timezone_set('PRC');

/**
 * 免转钱包 商户请求平台 PHP Demo
 * Class newngapi
 * @version (PHP 5 >= 5.3.0, PHP 7)
 * www.neapi.com
 */
class newngapi{

    protected  $api_account;
    protected  $sign_key;

    protected  $register_url;               // 创建会员账户
    protected  $login_url;                  // 获取游戏登录地址
    protected  $trans_url;                  // 额度转换
    protected  $balance_url;                // 免转钱包余额查询（玩家所在游戏平台+免转钱包）
    protected  $balance_plat_type_url;      // 会员游戏平台余额查询
    protected  $all_credit_url;             // 全部平台余额查询
    protected  $status_url;                 // 额度转换状态查询
    protected  $collect_url;                // 获取游戏记录

    protected  $plat_type;                  // 设置平台类型 全部小写

    public function __construct()
    {
        $this->plat_type = 'ag';

        $this->api_account="API帐号";
        $this->sign_key = "API密钥";

        $this->register_url = "http://域名/v1/user/register";
        $this->login_url = "http://域名/v1/user/login";
        $this->trans_url = 'http://域名/v1/wallet/trans';
        $this->balance_url = 'http://域名/v1/wallet/balance';
        $this->balance_plat_type_url = 'http://域名/v1/user/balance';
        $this->status_url = 'http://域名/v1/wallet/status';
        $this->all_credit_url= 'http://域名/v1/user/all-credit';
        $this->collect_url = 'http://域名/v1/user/record';
    }



    protected  $game_type_live = '1';     //真人娱乐
    protected  $game_type_slot = '2';     //老虎机
    protected  $game_type_lottery = '3';  //彩票
    protected  $game_type_sports = '4';   //体育
    protected  $game_type_esports = '5';  //电竞
    protected  $game_type_fishing = '6';  //捕鱼
    protected  $game_type_poker = '7';    //棋牌

    /**
     * @param $username
     * @return mixed
     * 注册
     */
    public function register($username){
        $code = md5($this->sign_key.$this->api_account.$this->plat_type.$username);
        $data = array(
            "username"=>$username,
            "plat_type"=>$this->plat_type,
            "sign_key"=>$this->sign_key,
            "code"=>$code
        );
        $res = $this->sendRequest($this->register_url, $data);
        return $res;
    }

    /**
     * @param $username
     * @param int $isMobileUrl
     * @param string $gameCode
     * @return mixed
     * 获取登录链接
     */
    public function login($username,$isMobileUrl=0,$gameCode=""){
        $code = md5($this->sign_key.$this->api_account.$username.$this->plat_type.$isMobileUrl);
        $data = array(
            "username"=>$username,
            "plat_type"=>$this->plat_type,
            "game_type"=>$this->game_type_live,
            "game_code"=>$gameCode,
            "sign_key"=>$this->sign_key,
            "is_mobile_url"=>$isMobileUrl,
            "code"=>$code
        );
        $res = $this->sendRequest($this->login_url, $data);
        return $res;
    }

    /**
     * @param $username
     * @param $money
     * @param $client_transfer_id
     * @return mixed
     * 更新中心钱包额度
     */
    public function trans($username,$money,$client_transfer_id){
        $code = md5($this->sign_key.$this->api_account.$username.$money.$client_transfer_id);
        $data = array(
            "username"=>$username,
            "money"=>$money,
            "client_transfer_id"=>$client_transfer_id,
            "sign_key"=>$this->sign_key,
            "code"=>$code
        );

        $res = $this->sendRequest($this->trans_url, $data);
        return $res;
    }


    /**
     * @param $username
     * @param $client_transfer_id
     * @return mixed
     * 查询中心钱包更新状态
     */
    public function status($username,$client_transfer_id){
        $code = md5($this->sign_key.$this->api_account.$username.$client_transfer_id);
        $data = array(
            "username"=>$username,
            "client_transfer_id"=>$client_transfer_id,
            "sign_key"=>$this->sign_key,
            "code"=>$code
        );
        $res = $this->sendRequest($this->status_url, $data);
        return $res;
    }

    /**
     * @param $username
     * @return mixed
     * 查询余额  会返回玩家当前所在游戏 和 在中心钱包的余额和
     */
    public function balance($username){
        $code = md5($this->sign_key.$this->api_account.$username);
        $data = array(
            "username"=>$username,
            "sign_key"=>$this->sign_key,
            "code"=>$code
        );
        $res = $this->sendRequest($this->balance_url, $data);
        return $res;
    }


    /**
     * @param $username
     * @return mixed
     * 根据游戏平台返回单个游戏的玩家余额
     */
    public function platTypeBalance($username){
        $code = md5($this->sign_key.$this->api_account.$username.$this->plat_type);
        $data = array(
            "username"=>$username,
            "plat_type"=>$this->plat_type,
            "sign_key"=>$this->sign_key,
            "code"=>$code
        );
        $res = $this->sendRequest($this->balance_plat_type_url, $data);
        return $res;
    }

    /**
     * @return mixed
     * 查询商户平台额度
     */
    public function credit(){
        $code = md5($this->sign_key.$this->api_account);
        $data = array(
            "sign_key"=>$this->sign_key,
            "code"=>$code
        );
        $res = $this->sendRequest($this->all_credit_url, $data);
        return $res;
    }

    /**
     * @param $startTime
     * @param $endTime
     * @param int $page
     * @param int $limit
     * @return mixed
     * 获取游戏记录
     */
    public function record($startTime,$endTime,$page=1,$limit=15){
        $code = md5($this->sign_key.$this->api_account.$this->plat_type.$startTime.$endTime);
        $data = array(
            "plat_type"=>$this->plat_type,
            "page"=>$page,
            "limit"=>$limit,
            "startTime"=>$startTime,
            "endTime"=>$endTime,
            "game_type"=>$this->game_type_slot,
            "sign_key"=>$this->sign_key,
            "code"=>$code
        );
        $res = $this->sendRequest($this->collect_url, $data);
        return $res;
    }


    private function sendRequest($url,$post_data=array()){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $contents = curl_exec($ch);
        curl_close($ch);
        print_r($contents);die;
        return json_decode ($contents, TRUE);
    }

}

/**
 * 使用demo
 */
$api = new newngapi();
$res = $api->login("username");
print_r($res);